<?php
/**
 * @name: template.class.php
 * @version: 1.0
 * @package: PHP MVC Framework
 * @uses: Handles template output
 * @copyright: MIT
 * @author Andrew Lowther
 */

class Template {

    /**
     * @the registry
     * @access private
     */
    private $registry;

    /**
     * @var array
     * @access private
     */
    private $template;

    /**
     * @method __construct
     * @access public
     * @return void
     */
    public function __construct($registry) {

        $this->registry = $registry;

    }

    /**
     * @set undefined vars
     * @param string $index
     * @param mixed $value
     * @return void
     */
    public function __set($index, $value) {

        $this->template->$index = $value;

    }

    /**
     * @function show
     * @return void
     */
    public function show($name) {

        if (!empty($_GET['rt'])) {

            // Get the url
            $url = $_GET['rt'];

            // Explode it to get the view directory
            $dir = explode('/', $url);

            // Set up the path
            $path = library . 'views/' . $dir[0] . '/' . $name . '.phtml';

        } else {

            $path = library . 'views/' . 'index/' . $name . '.phtml';

        }

        // Check if the file exists
        if (file_exists($path) == false) {

            try{

                throw new Exception('Template not found in: ' . $path);

            } catch (Exception $e) {

                echo '<h2>Message</h2>';
                echo $e->getMessage();

                echo '<h2>Stack Trace</h2>';
                echo '<pre>' . $e->getTraceAsString() . '</pre>';

            }

        } else {

            // Load variables
            foreach($this->template as $key => $value) {

                $key = $value;

            }

            // Include the file
            include $path;

        }

    }

    /**
     * @method appendStyle
     * @return string
     */
    public function appendStyle($style) {

        echo '<link rel="stylesheet" media="all" type="text/css" href="' . $style . '">';

    }

    /**
     * @method appendJava
     * @return string
     */
    public function appendJava($java) {

        echo '<script type="text/javascript" src="' . $java . '"></script>';

    }

    /**
     * @return string
     * @todo commentation
     */
    public function doctype($doctype) {

        switch($doctype) {

            case "HTML4_STRICT":
                echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
            break;

            case "HTML4_TRANS":
                echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
            break;

            case "HTML4_FRAME":
                echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
            break;

            case "XHTML1_STRICT":
                echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
            break;

            case "XHTML1_TRANS":
                echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
            break;

            case "XHTML1_FRAME":
                echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
            break;

            case "HTML5":
                echo '<!DOCTYPE html>';
            break;

        }

    }

}

?>
